﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Default3 : System.Web.UI.Page
{
    DataTable dtAgentProfile, dtListingInfo;
    int listingId;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            listingId = Convert.ToInt32(Request.QueryString["ListingId"].ToString());
            Session["ListingId"] = listingId;

            GetuserImages(listingId);
            setControlValues(listingId);
            setHyperLink();
        }

    }

    private void GetuserImages(int listingId)
    {
        DataTable dtPhotos = new DataTable();
        dtPhotos = BuddyAgentDAL.GetListingAllPhotos(listingId);

        int count = dtPhotos.Rows.Count;
        if (count != 0)
        {
            var finalScript = "<div id=\"gallery\" class=\"ad-gallery\"><div class=\"ad-image-wrapper\"></div><div class=\"ad-controls\"></div><div class=\"ad-nav\"><div class=\"ad-thumbs\"><ul class=\"ad-thumb-list\">";

            for (var i = 0; i < count; i++)
            {
                string url = dtPhotos.Rows[i][1].ToString();//.Remove(0, 2);
                finalScript = finalScript + "<li>" + "<a href='" + url + "'><img src='" + url + "' width='90' height='60'  /></a></li>\n";
            }


            finalScript = finalScript + "</ul></div></div></div>";

            container.InnerHtml = finalScript;
        }
        else
        {


        }

    }

    private void setControlValues(int listingId)
    {
        if (Session["Facebook"] != null)
        {
            if (Session["Facebook"].ToString() == "Can Edit")
                btnEditPost.Visible = true;
        }
        dtListingInfo = new DataTable();
        dtListingInfo = BuddyAgentDAL.GetParticularListingInfo(listingId);

        dtAgentProfile = new DataTable();
        dtAgentProfile = BuddyAgentDAL.GetBuddyAgentInfo(Convert.ToInt32(dtListingInfo.Rows[0][1]));

        DataTable dtCurrentPreviousListing = new DataTable();
        dtCurrentPreviousListing = BuddyAgentDAL.GetCurrentPreviousListing(Convert.ToInt32(dtListingInfo.Rows[0][1]));

        lblActiveListingCount.Text = "(" + dtCurrentPreviousListing.Rows[0][1].ToString() + ")";
        lblPreviousListingCount.Text = "(" + dtCurrentPreviousListing.Rows[0][2].ToString() + ")";

        imgUsr.ImageUrl = dtAgentProfile.Rows[0][2].ToString();

        lblProfileMsg.Text = dtAgentProfile.Rows[0][1].ToString() + ".<br/><br/>";

        DataSet dsAgentInformation = new DataSet();
        dsAgentInformation = BuddyAgentDAL.GetBuddyAgentInfoBasedOnFBId(dtAgentProfile.Rows[0][3].ToString());

        if (dsAgentInformation.Tables[0].Rows.Count != 0)
        {
            DateTime dt = DateTime.Parse(dsAgentInformation.Tables[0].Rows[0][5].ToString());

            lblProfileMes.Text = "Member Since " + dt.Year + ".<br/>";
            lblFriends.Text = "I have " + dsAgentInformation.Tables[1].Rows[0][0].ToString() + " Friends.<br/>";
        }

        lblText.Text = "Rentals: " + dtListingInfo.Rows[0][2].ToString();
        //imgHouse.ImageUrl = dtListingInfo.Rows[0][3].ToString();

        lblPrice.Text = "Rs. " + dtListingInfo.Rows[0][10].ToString();
        lblBR_BA.Text = dtListingInfo.Rows[0][13].ToString() + " BR, " + dtListingInfo.Rows[0][14].ToString() + " BA";

        lblLocation.Text = dtListingInfo.Rows[0][9].ToString() + ", " + dtListingInfo.Rows[0][8].ToString();

        lblDescription.Text = dtListingInfo.Rows[0][11].ToString();

        lblPropertyType.Text = dtListingInfo.Rows[0][4].ToString();

        lblSqFeets.Text = dtListingInfo.Rows[0][15].ToString() + "Sq. ft.";

        lblPetsAllowed.Text = dtListingInfo.Rows[0][17].ToString();

        lblCars.Text = dtListingInfo.Rows[0][18].ToString();

        lblWatchman.Text = dtListingInfo.Rows[0][19].ToString();

        if (dtListingInfo.Rows[0][21].ToString() != "")
            lblPhoneNo.Text = dtListingInfo.Rows[0][21].ToString();
        else
            lblPhone.Visible = false;

        if (Convert.ToInt32(dtListingInfo.Rows[0][22].ToString()) == 1)
        {
            lblAdd.Visible = true;
            lblAddress.Visible = true;
            lblAddress.Text = dtListingInfo.Rows[0][16].ToString();
        }
        else
        {
            lblAdd.Visible = false;
            lblAddress.Visible = false;
        }

        string comment = "<div id='fb-root'></div><script src='http://connect.facebook.net/en_US/all.js#xfbml=1'></script><fb:comments href=\"www.buddyAgent.com/ParticularListingInformation.aspx?id=" + listingId + "\" num_posts=\"2\" width=\"350\"></fb:comments>";
        FacebookComments.InnerHtml = comment;

        string like = "<div id='fb-root'></div><script src='http://connect.facebook.net/en_US/all.js#appId=202706659773465&amp;xfbml=1'></script><fb:like href=\"www.buddyAgent.com/ParticularListingInformation.aspx?id=" + listingId + "\"' send='false' width='450' show_faces='true' font=''></fb:like>";
        FacebookLike.InnerHtml = like;
    }

    protected void linkActiveListing_Click(object sender, EventArgs e)
    {
        listingId = Convert.ToInt32(Session["ListingId"].ToString());

        dtListingInfo = new DataTable();
        dtListingInfo = BuddyAgentDAL.GetParticularListingInfo(listingId);

        int clientId = Convert.ToInt32(dtListingInfo.Rows[0][1]);

        Response.Redirect("~/Default.aspx?ClientId=" + clientId + "&Active=1");
    }

    protected void linkPreviousListing_Click(object sender, EventArgs e)
    {
        listingId = Convert.ToInt32(Session["ListingId"].ToString());

        dtListingInfo = new DataTable();
        dtListingInfo = BuddyAgentDAL.GetParticularListingInfo(listingId);

        int clientId = Convert.ToInt32(dtListingInfo.Rows[0][1]);

        Response.Redirect("~/Default.aspx?ClientId=" + clientId + "&Active=-1");
    }

    private void setHyperLink()
    {
        listingId = Convert.ToInt32(Session["ListingId"]);
        dtListingInfo = new DataTable();
        dtListingInfo = BuddyAgentDAL.GetParticularListingInfo(listingId);

        dtAgentProfile = new DataTable();
        dtAgentProfile = BuddyAgentDAL.GetBuddyAgentInfo(Convert.ToInt32(dtListingInfo.Rows[0][1]));


        hideFBID.Value = dtAgentProfile.Rows[0][3].ToString();

    }

    protected void btnEditPost_Click(object sender, EventArgs e)
    {

        int listing = Convert.ToInt32(Request.QueryString["ListingId"].ToString());

        Response.Redirect("~/PostListingInformation.aspx?EditListingId=" + listing);
    }


}